package ldkxingzhe.ldkxingzhedemo.view.dialogs;

import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import ldkxingzhe.ldkxingzhedemo.R;

/**
 * Created by xingzhe on 15/11/11.
 */
public class ClassChoiceDialogFragments extends DialogFragment {

    private final String TAG = ClassChoiceDialogFragments.class.getSimpleName();

    private String json;

    // MARK: Global variable
    private List<Map.Entry<String, JsonElement>> jsonList;
    private List<List<String>> mTitleList;

    // UI Elements
    private TabLayout tabLayout;
    private ViewPager viewPager;

    // 传值常量
    private static final String JSON_STRING = "json_string";

    // 屏幕相关
    private int height;

    /**
     * 静态方法, 主要目的是用于传值
     * @param json 需要显示的json串
     * @return 返回当前的DialogFragments
     * */
    public static ClassChoiceDialogFragments newInstance(String json){
        ClassChoiceDialogFragments fragments = new ClassChoiceDialogFragments();
        Bundle args = new Bundle();
        args.putString(JSON_STRING,json);
        fragments.setArguments(args);
        return fragments;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        json = args.getString(JSON_STRING);
        if(json == null) throw new IllegalArgumentException("the json is null");
        JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
        Set<Map.Entry<String,JsonElement>> jsonSet = jsonObject.entrySet();
        jsonList = new ArrayList(jsonSet);
        mTitleList = new ArrayList<>();
        for(Map.Entry<String, JsonElement> entry : jsonList){
            JsonArray jsonArray = entry.getValue().getAsJsonArray();
            List<String> tmpList = new ArrayList<>();
            for(JsonElement jsonElement : jsonArray){
                tmpList.add(jsonElement.getAsString());
            }
            mTitleList.add(tmpList);
        }

        pageAdapter = new MyPagerAdapter();
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        height = metrics.heightPixels * 2 /3;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog =  super.onCreateDialog(savedInstanceState);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.class_choice_dialog, null);
        tabLayout = (TabLayout)layout.findViewById(R.id.tab_layout);
        viewPager = (ViewPager)layout.findViewById(R.id.view_pager);

        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        viewPager.setAdapter(pageAdapter);
        tabLayout.setupWithViewPager(viewPager);

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height));

        return dialog;
    }

    private PagerAdapter pageAdapter;
    class MyPagerAdapter extends PagerAdapter {

        private List<ListView> listViewList;
        MyPagerAdapter(){
            int length = jsonList.size();
            listViewList = new ArrayList<>(length);
            for(int i = 0; i < length; i++){
                ListView tmpListView = new ListView(getActivity());
                listViewList.add(tmpListView);
            }

        }
        @Override
        public int getCount() {
            return jsonList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ListView listView = listViewList.get(position);
            listView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, mTitleList.get(position));
            container.addView(listView);
            listView.setAdapter(adapter);
            return listView;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return jsonList.get(position).getKey();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(listViewList.get(position));
        }

    };
}
